import 'dart:math';
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';

class NumberKeyboardWidget extends StatefulWidget {
  final void Function(String outPut) onTap;

  // 删除功能按钮内容自定义
  final String languageDelete;

  // 默认按钮随机排布
  final bool isRandom;
  final Color backgroundColor;
  final Color itemColor;
  final Color textColor;

  NumberKeyboardWidget(
      {Key key,
      @required this.onTap,
      this.isRandom = true,
      this.languageDelete = '删除',
      this.backgroundColor = const Color(0xFFBDBDBD),
      this.itemColor = Colors.white,
      this.textColor = Colors.white})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _NumberKeyboardWidgetState();
}

class _NumberKeyboardWidgetState extends State<NumberKeyboardWidget> {
  List _number = [];
  List _outPutNumber = [];
  String outPutStr = '';
  String _languageRet;
  Color _backgroundColorRet;
  bool _isRandom;
  Color _itemColor;
  Color _textColor;

  @override
  void initState() {
    super.initState();

    _isRandom = widget.isRandom;
    _languageRet = widget.languageDelete;
    _backgroundColorRet = widget.backgroundColor;
    _itemColor = widget.itemColor;
    _textColor = widget.textColor;
    for (var i = 1; i <= 9; i++) {
      _number.add(i);
    }
    if (_isRandom) {
      /// 默认数字随机排布
      for (var i = 0; i < _number.length; i++) {
        int random = Random().nextInt(_number.length);
        dynamic t = _number[i];
        _number[i] = _number[random];
        _number[random] = t;
      }
      _number.add('C');
      _number.add('0');
      _number.add(_languageRet);

      int random = Random().nextInt(10);
      dynamic tmF = _number[10];
      _number[10] = _number[random];
      _number[random] = tmF;
    } else {
      _number.add('C');
      _number.add('0');
      _number.add(_languageRet);
    }
  }

  void _emmitEvent() {
    widget.onTap(outPutStr);
//    print('_emmitEvent$outPutStr');
    if (_outPutNumber.length == 6) {
      _outPutNumber?.clear();
    }
  }

  void _handlerResult(numberItem) {
    if (numberItem == 'C') {
      _outPutNumber.clear();
    } else {
      _outPutNumber.add(numberItem);
    }

    setState(() {
      outPutStr = _outPutNumber.join();
    });
    _emmitEvent();
  }

  void _handlerDelNumber() {
    if (_outPutNumber.length == 0) {
      return;
    }
    _outPutNumber.removeAt(_outPutNumber.length - 1);
    setState(() {
      outPutStr = _outPutNumber.join();
    });
    _emmitEvent();
  }

  Widget _buildKeysItem(index) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(5.0)),
//        color: Colors.white,
      ),
      width: (MediaQuery.of(context).size.width / 3 - 8.0) - 8.0,
      height: 44,
      child: Material(
        color: _itemColor,
        child: InkWell(
          highlightColor: Colors.grey[600],
          onTap: () {
            Vibration.vibrate(duration: 50);
            if (index != _languageRet) {
              _handlerResult(index);
            } else {
              _handlerDelNumber();
            }
          },
          child: Center(
              child: Text(
            index.toString(),
            style: TextStyle(
              fontSize: 14,
              color: _textColor,
            ),
          )),
        ),
      ),
    );
  }

  List<Widget> _buildWidgetList() {
    return _number.map((e) {
      return _buildKeysItem(e);
    }).toList();
  }

  Widget _buildKeys(context) {
    return Container(
      height: 44 * 4 + 40 + (MediaQuery.of(context).padding.bottom > 0 ? 44.0 : 0.0),
      width: MediaQuery.of(context).size.width,
      padding:
          EdgeInsets.only(left: 8, right: 8, top: 8, bottom: (MediaQuery.of(context).padding.bottom > 0 ? 44.0 : 8.0)),
      color: _backgroundColorRet,
      child: Center(
        child: Wrap(
          alignment: WrapAlignment.spaceBetween,
          spacing: 8.0,
          runSpacing: 8.0,
          children: _buildWidgetList(),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildKeys(context);
  }
}
